#include <1602.h>
#include <intrins.h>
#define delay() {_nop_();_nop_();_nop_();_nop_();_nop_();}
sbit en=P0^2;
sbit rw=P0^1;
sbit rs=P0^0;

uchar lcd_num=0; uint clear_time;
extern void delay_ms(uchar ms);

void LCD_Busy()
{
	uchar Hi,Lo;
	do
	{
		P0|=0xf0;
		rs=0;rw=1;
		en=1;delay();Hi=P0;delay();en=0;delay();
		en=1;delay();Lo=P0;delay();en=0;delay();
	}while(Hi&0x80);
}

void LCD_Write_Command(uchar cmd)
{
	LCD_Busy();
	P0=cmd&0xf0|0x04;
	delay();en=0;delay();
	P0=cmd<<4|0x04;
	delay();en=0;delay();
}

void LCD_Write_Data(uchar date)
{
	LCD_Busy();
	P0=date&0xf0|0x05;
	delay();en=0;delay();
	P0=date<<4|0x05;
	delay();en=0;delay();
}

void LCD_Init()
{
	P0=0xff;
	P0=0x24;
	en=0;
	LCD_Write_Command(0x28);delay_ms(15);
	LCD_Write_Command(0x0c);delay_ms(15);
	LCD_Write_Command(0x06);delay_ms(15);
	LCD_Write_Command(0x01);delay_ms(15);
	LCD_Write_Command(0x02);delay_ms(15);
	
}

void LCD_Show_String(uchar r,uchar c,uchar *str)
{
	uchar i=0;
	code uchar DDRAM[]={0x80,0xc0};
	LCD_Write_Command(DDRAM[r]|c);

	for(i=0;i<16&&str[i]!='\0';i++)
	 LCD_Write_Data(str[i]);
}
